<?php

// 商品分列表

namespace app\console\model;

use think\Model;
use think\Request;
use app\common\trilateral\Classify as ClassifyAlias;

class ShopGoodsType extends Model
{
    // 模型事件
    protected static function init()
    {
        // 写入前
        self::event('before_write', function ($data) {
            $data->parentStr = parentStr(Request::instance()->param('parenTid'), 'shop_goods_type', Request::instance()->param('id'), Request::instance()->param('repId'));
            $data->count = self::CountAttr();
            $data->picUrl = uploadImg('picUrl', 'shop');
            $data->update_time = date('Y-m-d', time());
        });
    }

    // count
    protected static function CountAttr()
    {

        if (Request::instance()->param('id'))
            $count = ClassifyAlias::countUpdate(Request::instance()->param('id'), Request::instance()->param('parenTid'), 'shop_goods_type');
        else
            $count = ClassifyAlias::countAdd(Request::instance()->param('parenTid'), 'shop_goods_type');

        // 判断是否大于2级
        if ($count > '2') {

            fail('最大只能是二级分类');

        }

        return $count;
    }

    // 删除方法
    public static function amputate($id)
    {

        if (self::where('parentStr', 'like', '%,' . $id . ',%')->whereOr('id', $id)->delete()) {
            return true;
        }

        return false;

    }

    // 全部删除方法
    public static function amputateAll($id)
    {

        foreach ($id as $value) {
            self::where('parentStr', 'like', '%,' . $value . ',%')->whereOr('id', $value)->delete();
        }

        return true;

    }
}